<?php
class raid_data extends data 
{
	protected function getAllRaidList()
	{
		$raid = $this->query("SELECT raidid as id, raidname, raidmaxpg FROM raid");
		while(list($id, $name, $maxpg) = $this->fetch_row($raid))
		{
			$raidSetupArray[$id]['id'] = $id;
			$raidSetupArray[$id]['raidname'] = $name;
			$raidSetupArray[$id]['raidmaxpg'] = $maxpg;
			$setup = $this->query("SELECT r.rolename, s.max FROM raidsetup s, role r WHERE r.roleid = s.roleid and s.raidid=$id");
			while(list($rolename, $maxpg) = $this->fetch_row($setup))
			{
				$raidSetupArray[$id]['raidmax-'.$rolename] = $maxpg;
			}
		}
		
		return $raidSetupArray;
	}
	
	protected function getRaid()
	{
		$this->query("SELECT raidid as id, raidname, raidmaxpg FROM raid WHERE raidid = {$_POST['raidid']}");
		list($id, $name, $maxpg) = $this->fetch_row($raid);
		
		$raidSetupArray['id'] = $id;
		$raidSetupArray['raidname'] = $name;
		$raidSetupArray['raidmaxpg'] = $maxpg;
		$setup = $this->query("SELECT r.rolename, s.max FROM raidsetup s, role r WHERE r.roleid = s.roleid and s.raidid=$id");
		while(list($rolename, $maxpg) = $this->fetch_row($setup))
		{
			$raidSetupArray['raidmax-'.$rolename] = $maxpg;
		}
		
		
		return $raidSetupArray;
	}
	
	protected function getRaidSetup($raidid)
	{
		$this->query("SELECT roleid, max FROM raidsetutp WHERE raidid = $raidid");
		return $this->fetch_all_row();
	}
	
	protected function setRaid()
	{
		
		$roledata = $this->getAllRoleClean();
		
		if(isset($_POST['raidid']) and $_POST['raidid'] != null)
		{
			if($this->query("UPDATE raid SET raidname = '{$_POST['raidname']}', raidmaxpg = '{$_POST['raidmaxpg']}' WHERE raidid = '{$_POST['raidid']}'")){
				foreach($roledata as $rowRole){
					if(!$this->query("UPDATE raidsetup SET max = {$_POST['raidmax-'.$rowRole['rolename']]} WHERE raidid = {$_POST['raidid']} and roleid = {$rowRole['roleid']}"))
						return $this->error;
				}
			}
			else
				return $this->error;
			
		}
		else 
		{
			if($this->query("INSERT INTO raid SET raidname = '{$_POST['raidname']}', raidmaxpg = '{$_POST['raidmaxpg']}'")){
				$raidid = $this->insert_id;
				foreach($roledata as $rowRole){
					if(!$this->query("INSERT INTO raidsetup SET raidid = $raidid, roleid = {$rowRole['roleid']}, max = {$_POST['raidmax-'.$rowRole['rolename']]}"))
						return $this->error;
				}
			}
			else
				return $this->error;
		}
	}
	
	protected function deleteRaid()
	{
		if($this->query("DELETE FROM raid WHERE raidid = '{$_POST['raidid']}'")){
			if(!$this->query("DELETE FROM raidsetup WHERE raidid = {$_POST['raidid']}"))
				return $this->error;
		}
		else
			return $this->error;
	}
}
?>